package com.ungs.form;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.ungs.bo.VarianteBO;
import com.ungs.bo.impl.VarianteBOImpl;
import com.ungs.entities.Variante;
import com.ungs.utils.ValidacionCampo;

public class AddVariante extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtCodigo;
	private JTextField txtDescripcion;
	private VarianteBO varianteBO;
	private List<Variante> varianteList;

	public AddVariante(JFrame parent) {
		super(parent, true);
		setResizable(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Agregar Variante");
		initForm();
		varianteBO = new VarianteBOImpl();
		varianteList = varianteBO.getAllVariante();
		iniciarCodigoVariante();
	}

	private void initForm() {
		setBounds(100, 100, 332, 181);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblAgregarVariante = new JLabel("Agregar Variante");
		lblAgregarVariante.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblAgregarVariante.setBounds(10, 11, 143, 32);
		contentPanel.add(lblAgregarVariante);

		JLabel lbCodigo = new JLabel("Codigo:");
		lbCodigo.setBounds(10, 61, 46, 14);
		contentPanel.add(lbCodigo);

		txtCodigo = new JTextField();
		txtCodigo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo
						.verificarLargoCamposJTextField(e, txtCodigo, 10);
			}
		});
		txtCodigo.setBounds(88, 54, 103, 20);
		contentPanel.add(txtCodigo);

		JLabel lbDescripcion = new JLabel("Descripcion:");
		lbDescripcion.setBounds(10, 89, 69, 14);
		contentPanel.add(lbDescripcion);

		txtDescripcion = new JTextField();
		txtDescripcion.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e,
						txtDescripcion, 50);
			}
		});
		txtDescripcion.setBounds(88, 86, 197, 20);
		contentPanel.add(txtDescripcion);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Agregar");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						eventoBotonAceptar();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancelar");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						eventoBotonCancelar();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

	private void iniciarCodigoVariante() {
		Long nuevoId = varianteBO.getUltimoId() + 1;

		String codigoFor = String.valueOf(nuevoId);
		for (int i = 0; i < 3; i++) {
			if (codigoFor.length() != 3) {
				codigoFor = "0" + codigoFor;
			}

		}

		txtCodigo.setText("VAR" + codigoFor);
		txtCodigo.selectAll();
	}

	private void eventoBotonAceptar() {
		if (!txtCodigo.getText().trim().isEmpty()
				&& !txtDescripcion.getText().trim().isEmpty()) {
			String codigo = txtCodigo.getText().trim().toUpperCase();

			boolean existe = false;
			for (Variante variante : varianteList) {
				if (variante.getCodigo().equals(codigo)) {
					existe = true;
				}
			}

			if (!existe) {
				Variante variante = new Variante();
				variante.setCodigo(codigo);
				variante.setDescripcion(txtDescripcion.getText().toUpperCase());
				varianteBO.guardar(variante);
				JOptionPane.showMessageDialog(this,
						"Se ha agregado con exito.", "",
						JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "El codigo ya existe",
						"Advertencia", JOptionPane.WARNING_MESSAGE);
				txtCodigo.grabFocus();
				txtCodigo.selectAll();
			}
		}
	}

	private void eventoBotonCancelar() {
		this.setVisible(false);
		this.dispose();
	}

}
